"""
学生管理类模块，增删改查的逻辑实现
"""

from student import *


class StudentManagerSystem(object):

    def __init__(self):
        # 定义容器属性保存所有学生
        # 容器是一个字典类型
        # key是学生的id
        self.students = {}

    # start()方法是启动该管理系统的公有接口方法
    def start(self):
        self.__loadDate()
        self.__print_menu()

    # 显示菜单的方法
    def __print_menu(self):
        while True:
            print("欢迎使用【学生管理系统】 V1.0")
            print("--------------------")
            print("1:添加学生")
            print("2:删除学生")
            print("3:修改学生")
            print("4:查询学生")
            print("5:显示所有学生")
            print("6:退出系统")
            print("--------------------")
            print("请输入一个功能ID")
            self.__operator(int(input()))

    # 实现操作的方法
    def __operator(self, o_id):
        if o_id == 1:
            self.__add_student()
        elif o_id == 2:
            stu_id = input("请输入需要删除学生的id ")
            self.__del_student(stu_id)
        elif o_id == 3:
            stu_id = input("请输入学生的id ")
            self.__change_student(stu_id)
        elif o_id == 4:
            stu_id = input("请输入学生的id ")
            self.__show_student(stu_id)
        elif o_id == 5:
            self.__show_all_student()
        elif o_id == 6:
            self.__saveDate()
            exit()

    # 删除学生
    def __del_student(self, stu_id):

        if stu_id in self.students.keys():
            del self.students[stu_id]
            print("学生信息已删除")
        else:
            print("学生id不存在")

        self.__print_menu()

    # 添加学生
    def __add_student(self):
        stu_id = input("请输入学生的id ")
        stu_name = input("请输入学生的姓名 ")
        stu_age = input("请输入学生的年龄 ")
        student = Student(stu_id, stu_name, stu_age)
        self.students[stu_id] = student

    def __change_student(self, stu_id):
        if stu_id in self.students.keys():
            stu_name = input("请输入学生的姓名 ")
            stu_age = input("请输入学生的年龄 ")
            student = Student(stu_id, stu_name, stu_age)
            self.students[stu_id] = student
        else:
            print("学生id不存在")

    # 显示学生信息
    def __show_student(self, stu_id):
        if stu_id in self.students.keys():
            print(self.students[stu_id])
        else:
            print("学生id不存在")

    # 显示所有学生信息
    def __show_all_student(self):
        for x in self.students:
            print(self.students[x])

    # 数据持久化保存数据到本地txt文件
    def __saveDate(self):
        # 以写的方式打开一个文件
        file_w = open('data', 'w')
        stu_s = ''
        for stu in self.students.values():
            stu_s = stu_s + stu.stu_id + ',' + stu.stu_name + ',' + stu.stu_age + '\n'
        print(stu_s)
        file_w.write(stu_s)
        file_w.close()

    # 数据持久化读取本地txt文件
    def __loadDate(self):
        print('加载数据')
        file_r = None
        try:
            file_r = open('data', 'r')
        except Exception as e:
            print(e, '文件不存在')
        else:
            content = file_r.readlines()
            print(content)
            for x in content:
                split_info = x.replace('\n', '').split(',')
                self.students[split_info[0]] = Student(split_info[0], split_info[1], split_info[2])
        finally:
            if file_r is not None:
                file_r.close()
